package com.example.demo.反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @description:
 * @projectName:demo
 * @see:反射
 * @author:袁成龙
 * @createTime:2020/10/6 19:02
 * @version:1.0
 */
public class TestKK {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class<KK> kkClass = KK.class;
        Constructor<KK> constructor = kkClass.getDeclaredConstructor(String.class, int.class);
        constructor.setAccessible(true);
        KK kk1 = constructor.newInstance("555", 0000);


        Method setName = kkClass.getMethod("setName",String.class);
        setName.invoke(kk1,"------");
        System.out.println(kk1.getName());


        Field name = kkClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(kk1,"张三");
        System.out.println(kk1.getName());

    }

}